﻿using MultiDAQ_Analysis.Calculation.Calculators;
using MultiDAQ_Analysis.Calculation.Interfaces;
using MultiDAQ_Analysis.Calculation.Parameters;
using MultiDAQ_Analysis.Util;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MultiDAQ_Analysis.Calculation.Factory
{
    public static class MeasurementCalculatorFactory
    {
        // 修复：明确指定返回类型和using语句
        private static readonly Dictionary<string, Func<IMeasurementCalculator>> _calculators =
            new Dictionary<string, Func<IMeasurementCalculator>>
            {
                { "Background", () => new BackgroundMeasurementCalculator() as IMeasurementCalculator },
                { "WorkingSourceEfficiency", () => new WorkingSourceEfficiencyCalculator() as IMeasurementCalculator },
                { "StandardSourceEfficiency", () => new StandardSourceEfficiencyCalculator() as IMeasurementCalculator },
                { "WaterSample", () => new WaterSampleCalculator() as IMeasurementCalculator },
                { "BioSample", () => new BioSampleCalculator() as IMeasurementCalculator },
                { "GasSample", () => new GasSampleCalculator() as IMeasurementCalculator },
                { "EnvSample", () => new EnvSampleCalculator() as IMeasurementCalculator }
            };

        public static IMeasurementCalculator CreateCalculator(string measurementType)
        {
            if (_calculators.TryGetValue(measurementType, out var factory))
            {
                return factory();
            }

            throw new ArgumentException($"未知的测量类型: {measurementType}");
        }

        public static MeasurementParameters CreateDefaultParameters(string measurementType)
        {
            switch (measurementType)
            {
                case "Background":
                    return null; // 本底测量不需要参数
                case "WorkingSourceEfficiency":
                    return CreateWorkingSourceParameters();
                case "StandardSourceEfficiency":
                    return CreateStandardSourceParameters();
                case "WaterSample":
                    return new WaterSampleParameters();
                case "BioSample":
                    return new BioSampleParameters();
                case "GasSample":
                    return new GasSampleParameters();
                case "EnvSample":
                    return new EnvSampleParameters();
                default:
                    return null;
            }
        }

        private static WorkingSourceParameters CreateWorkingSourceParameters()
        {
            var parameters = new WorkingSourceParameters();

            for (int i = 0; i < AppConfig.ChannelCount; i++)
            {
                parameters.AlphaEmissionRates[i] = 1440; // 默认值
                parameters.BetaEmissionRates[i] = 1594;
            }

            return parameters;
        }

        private static StandardSourceParameters CreateStandardSourceParameters()
        {
            var parameters = new StandardSourceParameters();

            for (int i = 0; i < AppConfig.ChannelCount; i++)
            {
                parameters.AlphaSpecificActivity[i] = 12.5;
                parameters.BetaSpecificActivity[i] = 14.4;
                parameters.SourceMass[i] = 20.0;
            }

            return parameters;
        }

        public static string GetMeasurementType(UIButton selectedButton)
        {
            if (selectedButton == null) return "Background";

            switch (selectedButton.Name)
            {
                case "btnBackground":
                    return "Background";
                case "btnWorkingSource":
                    return "WorkingSourceEfficiency";
                case "btnStandardSource":
                    return "StandardSourceEfficiency";
                case "btnWaterSample":
                    return "WaterSample";
                case "btnBioSample":
                    return "BioSample";
                case "btnGasSample":
                    return "GasSample";
                case "btnEnvSample":
                    return "EnvSample";
                default:
                    return "Background";
            }
        }
    }
}